#include "Sql_list.h"

// 单链表的创建
Link_list *init_link_list(void)
{
    // 1 创建 头节点的动态空间
    Link_list *list = (Link_list *)malloc(sizeof(Link_list));
    if (NULL == list)
    {
        printf("创建失败\n");
        return NULL;
    }

    list->next = NULL;

    return list;
}

// 单链表的遍历
void print_link_list(Link_list *list)
{
    Node *ptr = list->next;
    for (size_t i = 0; ptr != NULL; i++)
    {
        printf("姓名:%s 性别:%s 年龄:%d 成绩:%d 学号:%d 密码：%s\n",
               ptr->data.name, ptr->data.sex, ptr->data.age,
               ptr->data.sco, ptr->data.id, ptr->data.password);
        ptr = ptr->next;
    }
}

// 单链表的销毁
void free_link_list(Link_list **list)
{
    if (NULL == *list)
    {
        printf("链表 为 NULL\n");
        return;
    }

    // 1 销毁结点
    Node *ptr_1 = (*list)->next;
    Node *ptr_2 = ptr_1->next;
    for (size_t i = 0; ptr_1 != NULL; i++)
    {
        free(ptr_1);
        // if 判断  判断 ptr2 = NULL 的时候 我们如何处理
        ptr_1 = ptr_2;
        if (NULL != ptr_2)
        {
            ptr_2 = ptr_2->next;
        }
    }

    // 2 销毁头结点
    free(*list);
    *list = NULL;
}

// 单链表的尾插
void tail_insert_node(Link_list **list, Elenemt_data data)
{
    // 创建新节点
    Node *new_node = (Node *)malloc(sizeof(Node));
    new_node->data = data;
    new_node->next = NULL;

    // 如果链表为空，将新节点设置为头节点
    if ((*list)->next == NULL)
    {
        (*list)->next = new_node;
    }
    else
    {
        // 否则，遍历链表，找到最后一个节点
        Node *temp = (*list)->next;
        while (temp->next != NULL)
        {
            temp = temp->next;
        }
        // 将新节点添加到最后一个节点的后面
        temp->next = new_node;
    }
}

// 单链表的尾删
void tail_delete_node(Link_list **list)
{
    if (NULL == *list || NULL == (*list)->next)
    {
        printf("链表为空或只有一个节点\n");
        return;
    }

    // 1. 找到倒数第二个节点
    Node *ptr_1 = (*list)->next;
    Node *ptr_2 = ptr_1->next;
    while (ptr_2 != NULL && ptr_2->next != NULL)
    {
        ptr_1 = ptr_2;
        ptr_2 = ptr_2->next;
    }

    // 2. 删除最后一个节点
    free(ptr_2);
    ptr_1->next = NULL;
}

// 单链表的插入
void insert_node(Link_list *list, Elenemt_data data, int index)
{
    if (NULL == list)
    {
        printf("表为NULL\n");
        return;
    }

    //创建新节点
    Node *new_node = (Node *)malloc(sizeof(Node));
    if (NULL == new_node)
    {
        printf("new_node 创建动态内存失败\n");
        return;
    }

    //写入数据
    new_node->next = NULL;
    new_node->data = data;

    //=================头插=================
    if (1 == index)
    {
        new_node->next = list->next;

        list->next = new_node;
        return;
    }

    //============任意位置插入===============
    Node *ptr = list->next;
    for (size_t i = 1; i < index - 1 && NULL != ptr; i++)
    {
        ptr = ptr->next;
    }

    if (NULL == ptr)
    {
        printf("ptr已经指向NULL\n");
        free(new_node);
        return;
    }

    new_node->next = ptr->next;
    ptr->next = new_node;
    return;
}

// 单链表的删除
void delete_node(Link_list *list, int index)
{
    if (NULL == list)
    {
        printf("表为NULL\n");
        return;
    }
    if (NULL == list->next)
    {
        printf("表为无数据\n");
        return;
    }

    Node *ptr_1 = list->next;
    Node *ptr_2 = NULL;

    for (size_t i = 0; i < index - 1 && NULL != ptr_1; i++)
    {
        ptr_1 = ptr_1->next;
    }
    if (NULL == ptr_1)
    {
        printf("ptr_1已经指向NULL\n");
        return;
    }
    ptr_2 = ptr_1->next;
    ptr_1->next = ptr_2->next;
    free(ptr_2);
    return;
}

// 单链表的查找
Node *find_node_id(Link_list *list, int index)
{
    if (NULL == list)
    {
        printf("表为NULL\n");
        return NULL;
    }
    if (NULL == list->next)
    {
        printf("表为无数据\n");
        return NULL;
    }

    Node *ptr = list->next;
    for (size_t i = 0; i < index; i++)
    {
        ptr = ptr->next;
    }
    return ptr;
}

Node *find_node_name(Link_list *list, char *name)
{
    if (NULL == list)
    {
        printf("表为NULL\n");
        return NULL;
    }
    if (NULL == list->next)
    {
        printf("表为无数据\n");
        return NULL;
    }
    Node *ptr = list->next;

    for (size_t i = 0; NULL != ptr; i++)
    {
        if (!strcmp(ptr->data.name, name))
        {
            return ptr;
        }
        ptr = ptr->next;
    }
    return NULL;
}

// 单链表的清空
void clear_link_list(Link_list *list)
{
    if (NULL == list)
    {
        printf("表为NULL\n");
        return;
    }
    if (NULL == list->next)
    {
        printf("表为无数据\n");
        return;
    }

    Node *ptr_1 = list->next;
    Node *ptr_2 = ptr_1->next;

    for (size_t i = 0; ptr_1 != NULL; i++)
    {
        free(ptr_1);

        ptr_1 = ptr_2;
        if (NULL != ptr_2)
        {
            ptr_2 = ptr_2->next;
        }
    }
    list->next = NULL;
}

// 单链表的合并
Link_list *merge_link_lists(Link_list *list1, Link_list *list2)
{
    if (NULL == list1 || NULL == list2)
    {
        printf("表为NULL\n");
        return NULL;
    }
    if (NULL == list1->next && NULL == list2->next)
    {
        printf("两个表为无数据\n");
        return NULL;
    }

    Link_list *list3 = init_link_list();
    Node *ptr_1 = list1->next;
    size_t i = 0;
    for (i = 1; NULL != ptr_1; i++)
    {
        insert_node(list3, ptr_1->data, i);
        ptr_1 = ptr_1->next;
    }

    ptr_1 = list2->next;
    for (; NULL != ptr_1; i++)
    {
        insert_node(list3, ptr_1->data, i);
        ptr_1 = ptr_1->next;
    }
    return list3;
}

//登陆
void sign_in(Link_list *list, char *name, char *password)
{
    if (NULL != find_node_name(list, name))
    {
        int ptr = strcmp(list->next->data.password, password);
        if (ptr == 0)
        {
            printf("登陆成功\t");
        }
        else
        {
            printf("登陆失败\n");
        }
    }
    else
    {
        printf("查无此人\n");
        printf("是否要注册Y/N\n");
        getchar();
        
    }
}

//判断身份
void Identity(Link_list *list, char *name)
{
    const char *admin_permission = "root";
    char *found_node = find_node_name(list, name)->data.permission;

    if (found_node != NULL && strcmp(found_node, admin_permission) == 0)
    {
        printf("管理员\n");
    }
    else
    {
        printf("普通用户\n");
    }
}

//注册
Link_list *create_node(Link_list *user)
{
    Link_list *new_node = (Link_list *)malloc(sizeof(Link_list));
    if (new_node == NULL)
    {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    new_node->next->data = user->next->data;
    new_node->next = NULL;
    return new_node;
}

Link_list *User_register(Link_list *list, Link_list *user)
{
    Link_list *new_node = create_node(user);
    if (list->next == NULL)
    {
        return new_node;
    }
    
    while (list->next != NULL)
    {
        list->next = list->next->next;
    }
    list->next->next = new_node->next;
    
    return list;
}